package com.gec.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.gec.entity.User;
import com.gec.utils.C3P0Utils;

public class UserDao {
	private QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
	public User findUserBUsernameAndPassword(String username, String password) throws SQLException {
		String[] args = { username, password };
		User user = queryRunner.query("select * from user where username =? and password=?", args,
				new BeanHandler<>(User.class));
		return user;

	}
	
    
    public User getUserByUsername(String username) {
	String sql = "select * from user where username = ? and password=?";
	User user = null;
	try {
	    user = queryRunner.query(sql, new BeanHandler<User>(User.class), username);
	} catch (SQLException e) {
	    e.printStackTrace();
	}
	return user;
    }
    
	public int addUser(User user) {
		QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
		String sql = "insert into user values (?,?,?,?,?,?,?,?,?,?,?)";
		String[] arr = { user.getUid(), user.getUsername(), user.getPassword(), user.getName(), user.getEmail(),
				user.getTelephone(), user.getBirthday(), user.getSex(), String.valueOf(user.getState()), user.getCode(),
				user.getAddress() };
		int row = 0;
		try {
			row = queryRunner.update(sql, arr);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return row;
	}

}
